# -  *  - coding:utf-8  -  *  -
import sys

from selenium.webdriver.common.by import By

from config.globalconfig import picture_add_user_path, add_user_path
from public.common.basepage import Page
from public.common.read_data import get_data


class AddUserPage(Page):
    url = "http://120.24.81.195/qrweb/admin/producer/add.html"

    def __init__(self, url=url):
        super().__init__(url=url)
        self.account = (By.ID, "username")
        self.password = (By.ID, "pwd")
        self.name = (By.ID, "name")
        self.sex = "gender"
        self.profession = (By.ID, "profession")
        self.company = (By.ID, "company")
        self.phone = (By.ID, "company")
        self.email = (By.ID, "email")
        self.coursetype = (By.ID, "phone")
        self.submit = (By.ID, "button")
        self.data = get_data(add_user_path)

    def new_account(self, account=""):
        if account and account != "":
            self.get_element(*self.account).send_keys(account)
        else:
            pass

    def new_password(self, password=""):
        if password and password != "":
            self.get_element(*self.password).send_keys(password)
        else:
            pass

    def new_name(self, name=""):
        if name and name != "":
            self.get_element(*self.name).send_keys(name)
        else:
            pass

    def new_sex(self, sex=""):
        if sex == "man" or sex == "":
            self.select(name=self.sex, type="index", value=0)
        elif sex == "women":
            self.select(name=self.sex, type="index", value=1)
        else:
            print("sex:错误")

    def new_profession(self, profession=""):
        if profession and profession != "":
            self.get_element(*self.profession).send_keys(profession)
        else:
            pass

    def new_company(self, company=""):
        if company:
            self.get_element(*self.company).send_keys(company)
        else:
            pass

    def new_phone(self, phone=""):
        if phone and phone != "":
            self.get_element(*self.phone).send_keys(phone)
        else:
            pass

    def new_email(self, email=""):
        if email and email != "":
            self.get_element(*self.email).send_keys(email)
        else:
            pass

    def new_user(self, info):
        self.new_account(info[0])
        self.new_password(info[1])
        self.new_name(info[2])
        self.new_sex(info[3])
        self.new_profession(info[4])
        self.new_company(info[5])
        self.new_phone(info[6])
        self.new_email(info[7])
        self.get_element(*self.submit).click()

    def is_add_user(self, info):
        self.new_user(info)
        text = self.get_alter_text()
        self.dirver.switch_to.alert.accept()
        func_name = sys._getframe().f_back.f_code.co_name
        self.save_picture(picture_add_user_path, func_name)
        return text

    def test_empty(self):
        # 为空提交
        text = self.is_add_user(self.data[1])
        print(text)


if __name__ == "__main__":
    a = AddUserPage()
    a.test_empty()
